<?php

require_once('./fpdf/tcpdf/tcpdf.php');

// create new PDF document
//$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf = new TCPDF($orientation = 'L', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);



/*
  // set document information
  $pdf->SetCreator(PDF_CREATOR);
  $pdf->SetAuthor('Nicola Asuni');
  $pdf->SetTitle('TCPDF Example 048');
  $pdf->SetSubject('TCPDF Tutorial');
  $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
 */
// set default header data
//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 04', PDF_HEADER_STRING);
$pdf->SetHeaderData("", "", "รายงานรายละเอียดประกอบเช็คหนี้", "");

// set header and footer fonts
//$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setHeaderFont(Array("thsarabunb", 'B', 22));

$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
//$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
// ---------------------------------------------------------
// set font
//$pdf->SetFont('helvetica', 'B', 20);
$pdf->SetFont('thsarabunb', 'B', 22);

// add a page
$pdf->AddPage();

//$pdf->Write(0, 'รายงานการจัดเช็ค', '', 0, 'L', true, 0, false, false, 0);
//$pdf->SetFont('helvetica', '', 8);
// -----------------------------------------------------------------------------


$pdf->SetFont('thsarabunb', 'B', 16);
$tbl_header = '
<table cellspacing="0" cellpadding="1" border="1" width="100%">

';

require_once("includes/connect.php");
require_once("includes/function.php");

$db = new myDBS;
$db->selectDB("account");

//$db2 = new myDBS;


$from = $_REQUEST["from"];
$to = $_REQUEST["to"];

$sql = "SELECT DISTINCT cheque_no
		FROM tbl_debt 
		WHERE cheque_no IS NOT NULL 
		AND print_cheque_date between '$from' and '$to' 
		ORDER BY cheque_no
		";
//echo $sql;
$result = $db->runSQL($sql);
$pdf->SetFont('thsarabunb', '', 16);
$i = 0;

while ($rows = $db->fetch_object($result)) {
    $i++;

    //$result1 = $db->runSQL($sql);


    $tbl_body = $tbl_body . '
		<tr>
			<td colspan="8">เลขที่เช็ค: ' . $rows->cheque_no . '</td>
		</tr>
		<tr>
			<td>#</td>
			<td>ธนาคาร</td>
			<td>สาขา</td>
			<td>เลขที่บัตรประชาชน</td>
			<td>ชื่อ นามสกุล</td>
                        <td>รายการ</td>
			<td>จำนวนเงิน</td>
			<td>หมายเหตุ</td>
		</tr>
		';
    $sql = "select distinct * from tbl_debt where cheque_no = '" . $rows->cheque_no . "'";
    $result1 = $db->runSQL($sql);
    //echo $sql;
    $j = 1;
    $num_rows = $db->num_rows($result1);
    $sum = 0;
    while ($rows1 = $db->fetch_object($result1)) {
        //$date = date_create($rows1->cheque_date);
        //$xxx=11111111111;
        //$wTax = $rows1->inpValues * $rows1->WithholdingTax / 100;
        //$db2->selectDB("hr_udoncity");
        //$sql = "select fname, lname from person where id = '".$rows1->id."'";
        //$result2 = $db1->runSQL($sql);
        //$rows2 =  $db1->fetch_object ( $result2 );
        if ($rows1->tax > 0) {
            $item = "ภาษี";
            $amount = $rows1->tax;
        } elseif ($rows1->aia > 0) {
            $item = "AIA";
            $amount = $rows1->aia;
        } elseif ($rows1->cremation2 > 0) {
            $item = "ก.ฌ. เทศบาล";
            $amount = $rows1->cremation2;
        } elseif ($rows1->cremation1 > 0) {
            $item = "ก.ฌ.";
            $amount = $rows1->cremation1;
        } elseif ($rows1->welfare > 0) {
            $item = "สวัสดิการ";
            $amount = $rows1->welfare;
        } elseif ($rows1->cooperative > 0) {
            $item = "สหกรณ์";
            $amount = $rows1->cooperative;
        } elseif ($rows1->loan > 0) {
            $item = "กู้เทศบาล";
            $amount = $rows1->loan;
        } elseif ($rows1->bongkum > 0) {
            $item = "ธอ/บงคำ";
            $amount = $rows1->bongkum;
        } elseif ($rows1->ghb > 0) {
            $item = "ธอส";
            $amount = $rows1->ghb;
        } elseif ($rows1->other2 > 0) {
            $item = "อื่นๆ";
            $amount = $rows1->other2;
        } else {
            $item = "ผิดพลาด";
            $amount = -1;
        }
        $tbl_body = $tbl_body . '
				<tr>
					<td align="center">' . $j . '</td>
					<td align="center">' . $rows1->bank_name . '</td>
					<td align="center">' . $rows1->branch_name . '</td>
					<td align="center">' . $rows1->card_id . '</td>
                                            <td align="center">&nbsp;</td>
					<td align="center">' . $item . '</td>
					<td align="right">' . $amount . '</td>
					<td align="center">&nbsp;</td>
				</tr>
				';

        $sum = $sum + $amount;
        if ($j == $num_rows) {
            $tbl_body = $tbl_body . '
						<tr>
							<td colspan="6" align="right">รวม</td>
							<td align="right">' . number_format($sum, 2) . '</td>
							<td align="right">&nbsp;</td>
							
						</tr>	
					';
        }
        $j++;
    }
}

$tbl_footer = '</table>';

//echo $tbl_header.$tbl_body.$tbl_footer;

$pdf->writeHTML($tbl_header . $tbl_body . $tbl_footer, true, false, false, false, '');

//$pdf->Output('example_048.pdf', 'I');
$pdf->Output();
?>